<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'users';
    
    protected $fillable = [
        'openid', 'nickname', 'avatar', 'total_score', 'win_count', 
        'lose_count', 'last_login_time'
    ];
    
    public $timestamps = false;
    
    protected $primaryKey = 'id';
    
    /**
     * 获取用户战绩
     */
    public function getWinRate()
    {
        $total = $this->win_count + $this->lose_count;
        if ($total == 0) return 0;
        return round(($this->win_count / $total) * 100, 2);
    }
    
    /**
     * 更新用户积分
     */
    public function updateScore($score)
    {
        $this->total_score += $score;
        
        if ($score > 0) {
            $this->win_count++;
        } else {
            $this->lose_count++;
        }
        
        $this->save();
    }
}